2079. 给植物浇水
https://leetcode-cn.com/problems/watering-plants/
- 提交时间:2021-11-23 17:39:33
- 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
- 内存消耗:2.7 MB, 在所有 Go 提交中击败了56.83%的用户
- 通过测试用例:40 / 40
func wateringPlants(plants []int, capacity int) (stepCount int) {
capacityNow := capacity
pPos := 0
stepCount = 0
n := len(plants)
for pPos < n {
// fmt.Printf("pPos:%+v\n", pPos)
//打水浇水
if capacityNow < plants[pPos] {
capacityNow = capacity - plants[pPos]
stepCount += pPos*2 + 1
pPos++
// fmt.Printf("1,stepCount:%+v,pPos:%+v\n", stepCount, pPos)
continue
}
//浇水
capacityNow -= plants[pPos]
stepCount++
pPos++
// fmt.Printf("2,stepCount:%+v,pPos:%+v\n", stepCount, pPos)
}
return stepCount
}